package com.samxcode.leetcode;

/**
 * Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could
 * be partially filled, where empty cells are filled with the character '.'.
 * 
 * @author Sam
 *
 */
public class ValidSudoku {

    public boolean isValidSudoku(char[][] board) {
        boolean[][] rowChecker = new boolean[9][9];
        boolean[][] colChecker = new boolean[9][9];
        boolean[][] blockChecker = new boolean[9][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int n = board[i][j] - 49;
                    if (rowChecker[i][n] == true || colChecker[j][n] == true
                            || blockChecker[i / 3 * 3 + j / 3][n] == true) {
                        return false;
                    } else {
                        rowChecker[i][n] = true;
                        colChecker[j][n] = true;
                        blockChecker[i / 3 * 3 + j / 3][n] = true;
                    }
                }
            }
        }
        return true;
    }
}
